package com.tuyuan.alramautorobot.time.autoDailyLog;

import com.tuyuan.alramautorobot.time.config.DailyLogConfig;
import com.tuyuan.alramautorobot.time.model.request.MessageBO;
import com.tuyuan.alramautorobot.time.model.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

@Slf4j
@Component
@EnableScheduling
public class AutoTask {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DailyLogConfig config;

    /**
     * 每十分钟执行一次
     */
    @Async
    @Scheduled(cron = "0 30 17 * * ?")
    public  void autoDailyLog(){
        log.info("日志填报提醒：start =========================");

        //企业微信webhook ：https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=74011b5b-fb1f-473f-9dec-e49e20216304
        //设置表头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-type", "application/json;charset=UTF-8");
        HttpEntity<MessageBO> requestEntity = new HttpEntity<>(config.getDailyLogAlarmQuest());
        ParameterizedTypeReference<Result> typeRef = new ParameterizedTypeReference<Result>() {};
        boolean flag1 = LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)));
        boolean flag2 = LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY)));
        if(flag2 || flag1){
            log.info("today is holiday ! no alarming");
            return;
        }
        for (int i = 0; i < 2; i++) {
            restTemplate.exchange(config.DailyLogRobotUrl, HttpMethod.POST, requestEntity,typeRef);
        }
    }



    /**
     * 每十分钟执行一次
     */
    @Async
    @Scheduled(cron = "0 40 8 * * ?")
    public  void autoOnClassClockLog(){
        log.info("上班卡提醒：start===================");
        //企业微信webhook ：https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=74011b5b-fb1f-473f-9dec-e49e20216304
        //设置表头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-type", "application/json;charset=UTF-8");
        HttpEntity<MessageBO> requestEntity = new HttpEntity<>(config.getAutoOnClockAlarmQuest());
        ParameterizedTypeReference<Result> typeRef = new ParameterizedTypeReference<Result>() {};
        boolean flag1 = LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)));
        boolean flag2 = LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY)));
        if(flag2 || flag1){
            log.info("today is holiday ! no alarming");
            return;
        }
        for (int i = 0; i < 1; i++) {
            restTemplate.exchange(config.AutoClockRobotUrl, HttpMethod.POST, requestEntity,typeRef);
        }
    }



    /**
     * 每十分钟执行一次
     */
    @Async
    @Scheduled(cron = "0 0 18 * * ?")
    public  void autoOffClassClockLog(){
        log.info("下班卡提醒：start===================");
        //企业微信webhook ：https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=74011b5b-fb1f-473f-9dec-e49e20216304
        //设置表头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-type", "application/json;charset=UTF-8");
        HttpEntity<MessageBO> requestEntity = new HttpEntity<>(config.getAutoOffClockAlarmQuest());
        ParameterizedTypeReference<Result> typeRef = new ParameterizedTypeReference<Result>() {};
        boolean flag1 = LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)));
        boolean flag2 = LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY)));
        if(flag2 || flag1){
           log.info("today is holiday ! no alarming");
           return;
        }
        for (int i = 0; i < 1; i++) {
            restTemplate.exchange(config.AutoClockRobotUrl, HttpMethod.POST, requestEntity,typeRef);
        }
    }

}
